#include "SessionEditor.h"
#include "SessionPanel.h"

#include "Util.h"
#include "UserConfig.h"
#include "MsnSession.h"
#include "MsnMessenger.h"

int ID_SESSION_EDITOR = wxNewId();

int ID_SESSION_CHANGE_FONT = wxNewId();
int ID_SESSION_SEND_FILE = wxNewId();

BEGIN_EVENT_TABLE(SessionEditor, SessionEditorCtrl)
	EVT_MENU(ID_SESSION_CHANGE_FONT, SessionEditor::OnChangeFont)
	EVT_MENU(ID_SESSION_SEND_FILE, SessionEditor::OnSendFile)

	//EVT_CONTEXT_MENU()
END_EVENT_TABLE()

SessionEditor::SessionEditor(wxWindow* parent,  SessionPanel* sp)
	:SessionEditorCtrl(parent, ID_SESSION_EDITOR, wxEmptyString,wxDefaultPosition, wxDefaultSize,
		wxVSCROLL|wxWANTS_CHARS|wxTE_AUTO_URL|wxTE_PROCESS_ENTER|wxTE_MULTILINE|wxTE_RICH2), 
		sessionPanel(sp)
{
	this->Connect(wxEVT_KEY_DOWN, wxKeyEventHandler(SessionEditor::OnKeyDown), NULL, this); 

	this->Connect(wxEVT_CONTEXT_MENU, wxContextMenuEventHandler(SessionEditor::OnContextMenu), NULL, this); 

	MsnMessenger* messenger = sessionPanel->GetSession()->GetMessenger();
	messageStyle = wxTextAttr(messenger->GetUserConfig()->GetMessageColor(), *wxWHITE, messenger->GetUserConfig()->GetMessageFont());
	
	this->SetDefaultStyle(messageStyle);
}

SessionEditor::~SessionEditor()
{

}

void SessionEditor::SetMessageStyle(wxTextAttr& attr)
{

}

void SessionEditor::OnContextMenu(wxContextMenuEvent& event)
{
	wxMenu menu;
	menu.Append(wxID_SELECTALL, _("Select All"));
    menu.Append(wxID_COPY, _("Copy"));
	menu.Append(wxID_CUT, _("Cut"));
    menu.Append(wxID_PASTE, _("Paste"));	
	 
    
	menu.AppendSeparator();
	menu.Append(ID_SESSION_CHANGE_FONT, _("Change Font"));
	menu.AppendSeparator();
	menu.Append(ID_SESSION_SEND_FILE, _("Send File"));
	
	if(!sessionPanel->GetSession()->IsAlive()){
		menu.Enable(ID_SESSION_CHANGE_FONT, false);	
		menu.Enable(ID_SESSION_SEND_FILE, false);	
	}

    PopupMenu(&menu);
}

void SessionEditor::OnKeyDown(wxKeyEvent& event)
{
	if(!this->IsEditable()) return;


	const wxChar ch = event.GetKeyCode();

    //long keycode = event.GetKeyCode();
	//DEBUG(wxString::Format(_T("input: %d"), keycode));	

	if(event.ControlDown() && ch == 'A'){
		DEBUG(_T("ctrl-a"));
		this->SetSelection(-1, -1);
		return;
	}

	if(event.ControlDown() && ch == 'C'){
		DEBUG(_T("ctrl-c"));
		this->Copy();
		return;
	}

	if(event.ControlDown() && ch == 'V'){
		DEBUG(_T("ctrl-v"));
		this->Paste();
		return;
	}

	if(event.ControlDown() && ch == 'X'){
		DEBUG(_T("ctrl-x"));
		this->Cut();
		return;
	}

	if(event.ControlDown() && ch == 'Y'){
		DEBUG(_T("ctrl-y"));
		this->Redo();
		return;
	}

	if(event.ControlDown() && ch == 'Z'){
		DEBUG(_T("ctrl-z"));
		this->Undo();
		return;
	}

//ctrl enter can not be detected?????
/*
	if(event.ControlDown() && ch == WXK_RETURN){  //ctrl does not work, use shift instead
		DEBUG(_T("Ctrl return"));

		return;
	}
*/


	//change line
	if (ch == WXK_RETURN){  //ctrl does not work, use shift instead
		//DEBUG(_T("shift return"));

		MsnMessenger* messenger = sessionPanel->GetSession()->GetMessenger();

		bool shiftEnter = messenger->GetUserConfig()->GetMessageShiftEnter();

		if(event.ShiftDown()){
			if(shiftEnter){
				SendMessage();
			}else{
				WriteText(_T("\r\n"));
			}
		}else{
			if(shiftEnter){
				WriteText(_T("\r\n"));
			}else{
				SendMessage();
			}
		}	

		#if defined(_USING_TEXTCTRL_) //set style again for wxTextCtrl
			this->SetDefaultStyle(messageStyle);
		#endif

		return;
	}

	event.Skip();
}

void SessionEditor::SendMessage()
{
	wxString value = GetValue().Trim(true).Trim(false);
	if(value.IsEmpty()) return;

	if(WSLEN(value) > MsnDef::MSN_MESSAGE_MAX_LENGTH){
			wxMessageBox(_("Message is too long"));
			return;
	}

	//check whether session is still alive 
	MsnMessenger* messenger = sessionPanel->GetSession()->GetMessenger();

		if(messenger->IsAlive()){
			sessionPanel->AppendInstantMessage(messenger->GetUserPassport(), value);
			sessionPanel->GetSession()->SendInstantMessage(value);
			//Clear();
			this->ChangeValue(_T(""));
		}	
}


void SessionEditor::OnChangeFont(wxCommandEvent& event)
{
    wxFontData data;
    data.SetColour(messageStyle.GetTextColour());
    data.SetInitialFont(messageStyle.GetFont());

    wxFontDialog dialog(this, &data);

    if (dialog.ShowModal() == wxID_OK)
    {
        wxFontData retData = dialog.GetFontData();

        messageStyle = wxTextAttr(retData.GetColour(), *wxWHITE, retData.GetChosenFont());
		this->SetDefaultStyle(messageStyle);
		this->SetStyle(0, GetLastPosition(), messageStyle);

        sessionPanel->SetMessageStyle(messageStyle);

		MsnMessenger* messenger = sessionPanel->GetSession()->GetMessenger();
		if(messenger->IsAlive()){
			messenger->GetUserConfig()->SetMessageColor(retData.GetColour());
			messenger->GetUserConfig()->SetMessageFont(retData.GetChosenFont());
		}
	}
}

void SessionEditor::OnSendFile(wxCommandEvent& event)
{
	MsnMessenger* messenger = sessionPanel->GetSession()->GetMessenger();
	
	wxFileDialog fileDialog(this, _("Select File"), _T(""), _T(""), _T("*.*"), wxFD_OPEN);
	if (fileDialog.ShowModal() == wxID_OK){
		//sessionPanel->GetSession()->SendFileInviteMessage(fileDialog.GetPath());

		//sessionPanel->GetSession()->SendP2PSessionInviteMessage(fileDialog.GetPath());

		sessionPanel->GetSession()->SendP2PDisplayPictureRequest();
	}
}